package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         64,
		Discription: "从左上角到右下角的最小路径和",
		Level:       2,
		Labels: map[string]int{
			"动态规划": 1,
			"空间复用": 1,
		},
	})
}

func MinPathSum(grid [][]int) int {
	m := len(grid)
	n := len(grid[0])
	dp := make([]int, n)
	dp[0] = grid[0][0]
	for j := 1; j < n; j++ {
		dp[j] = grid[0][j] + dp[j-1]
	}

	for i := 1; i < m; i++ {
		dp[0] += grid[i][0]
		for j := 1; j < n; j++ {
			dp[j] = grid[i][j] + min(dp[j], dp[j-1])
		}
	}

	return dp[n-1]
}
